/**
 * Created by liuqingling on 16/6/14.
 */
var express = require("express");
var httpProxy = require("http-proxy");
var webpack = require("webpack");
var path = require("path");
var webpackDevMiddleware = require("webpack-dev-middleware");
var webpackHotMiddleware = require("webpack-hot-middleware");
var webpackConfig = require("./webpack.config");


var app = express();
var apiProxy = httpProxy.createProxyServer();
var compiler = webpack(webpackConfig);
//app.set('views', path.join(__dirname));
//app.set('view engine', 'ejs');
// Start a webpack-dev-server
app.use(webpackDevMiddleware(compiler, {
    // server and middleware options
    publicPath: webpackConfig.output.publicPath,
}));

// Enables HMR
app.use(webpackHotMiddleware(compiler));
app.get('/',function(req,res){
    console.log('fuck');
    res.sendFile(__dirname+'/index.html');
});
// Proxy api requests
app.use("/token/*", function(req, res) {
    req.url = req.baseUrl; // Janky hack...
    req.headers.host='h5.api.esndev.com:6061';
    console.log(req.headers);
    console.log(req.baseUrl);
    apiProxy.web(req, res, {target: "http://h5.api.esndev.com:6061"});
});

app.listen(3000);
